package com.hehui.partimejob.view;

import android.content.Context;
import android.util.LayoutDirection;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hehui.partimejob.R;
import com.hehui.partimejob.uitls.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.Hutool;

public class InputView {

    private static final String TAG = "InputView";
    private static final int CONTAINER_TOP_MARGIN = 35;
    private static final int CONTAINER_BOTTOM_MARGIN = 43;
    private LinearLayout mContainer;
    private Context mContext;
    private List<Item> items;

    public LinearLayout getmContainer() {
        return mContainer;
    }

    public Context getmContext() {
        return mContext;
    }

    public List<Item> getItems() {
        return items;
    }

    public List<Item> addInputView(Context context, List<InputData> data) {
        mContext = context;
        mContainer = new LinearLayout(context);

        layoutContainer();

        return createItem(data);
    }

    private List<Item> createItem(List<InputData> data) {
        if (mContainer == null || mContext == null)
            throw new NullPointerException();
        if (data.size() == 0)
            return null;
        items = new ArrayList<>();
        for (InputData datum : data) {
            LinearLayout linearLayout = new LinearLayout(mContext);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            ImageView icon = new ImageView(mContext);
            EditText editText = new EditText(mContext);
            icon.setBackgroundResource(datum.icon);
            int iconWidth = (int) mContext.getResources().getDimension(R.dimen.small_icon);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(iconWidth, iconWidth);
            int iconMargin = DisplayUtil.dp2px(mContext, 10);
            layoutParams.setMargins(iconMargin, iconMargin, iconMargin, iconMargin);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            icon.setLayoutParams(layoutParams);
            editText.setId(datum.id);
            editText.setHint(datum.hint);
            editText.setBackgroundResource(R.color.transparent);
            editText.setHintTextColor(mContext.getColor(R.color.pri_gray));
            editText.setTextColor(mContext.getColor(R.color.black));
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
            layoutParams1.gravity = Gravity.CENTER_VERTICAL;
            layoutParams1.rightMargin = (int) mContext.getResources().getDimension(R.dimen.priMargin);
            editText.setMaxLines(1);
            editText.setLayoutParams(layoutParams1);
            linearLayout.addView(icon);
            linearLayout.addView(editText);
            mContainer.addView(linearLayout);

            Item item = new Item();
            item.setIcon(icon).setEditText(editText);
            items.add(item);
            if (data.indexOf(datum) != data.size() - 1) {
                View line = new View(mContext);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) mContext.getResources().getDimension(R.dimen.line));
                line.setBackgroundColor(mContext.getColor(R.color.pri_gray));
                line.setLayoutParams(layoutParams2);
                mContainer.addView(line);
            }

        }
        return items;
    }

    private void layoutContainer() {
        if (mContainer == null || mContext == null)
            throw new NullPointerException();
        mContainer.setBackgroundResource(R.drawable.input_container);
        mContainer.setOrientation(LinearLayout.VERTICAL);
        int left = (int) mContext.getResources().getDimension(R.dimen.priMargin);
        Log.d(TAG, "initInputView: " + mContext.getResources().getDimension(R.dimen.priMargin));
        int top = DisplayUtil.dp2px(mContext, CONTAINER_TOP_MARGIN);
        int bottom = DisplayUtil.dp2px(mContext, CONTAINER_BOTTOM_MARGIN);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, left, bottom);
        mContainer.setLayoutParams(layoutParams);
    }


    public static class InputData {
        int id;
        int icon;
        String hint;

        public InputData() {
        }

        public InputData(int id, int icon, String hint) {
            this.id = id;
            this.icon = icon;
            this.hint = hint;
        }

        public int getId() {
            return id;
        }

        public InputData setId(int id) {
            this.id = id;
            return this;
        }

        public int getIcon() {
            return icon;
        }

        public InputData setIcon(int icon) {
            this.icon = icon;
            return this;
        }

        public String getHint() {
            return hint;
        }

        public InputData setHint(String hint) {
            this.hint = hint;
            return this;
        }

        public InputData setHint(CharSequence hint) {
            this.hint = hint.toString();
            return this;
        }
    }

    public static class Item {
        ImageView icon;
        EditText editText;
        String value;

        public String getValue() {
            if (editText != null)
                return editText.getText().toString();
            return "";
        }

        public Item setValue(String value) {
            if (editText != null) {
                this.value = value;
                this.editText.setText(value);
            }
            return this;

        }

        public Item() {
        }

        public Item(ImageView icon, EditText editText) {
            this.icon = icon;
            this.editText = editText;
        }

        public ImageView getIcon() {
            return icon;
        }

        public Item setIcon(ImageView icon) {
            this.icon = icon;
            return this;
        }

        public EditText getEditText() {
            return editText;
        }

        public Item setEditText(EditText editText) {
            this.editText = editText;
            return this;
        }
    }
}
